package com.kermel.andromeda;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.kermel.andromeda.activities.alarm.add.AddAlarmActivity;
import com.kermel.andromeda.activities.alarm.list.action.ViewMyAlarmsAction;
import com.kermel.andromeda.activities.countdown.add.AddCountdownTimerActivity;
import com.kermel.andromeda.clock.view.ClockView;
import com.kermel.andromeda.data.persistence.AndromedaDatabaseManager;
import com.kermel.andromeda.data.settings.EditClockSettingsActivity;

public class Andromeda extends Activity {
    private static Context mainApplicationContext;
    private ClockView clockView;

    public Andromeda() {
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mainApplicationContext = getApplicationContext();
        
        AndromedaDatabaseManager.instance.initializeDatabase(this);
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        
        // TODO: Highly inefficient here, recreating the ClockView every time we resume. To be investigated.
        clockView = new ClockView(this);
        setContentView(clockView);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_options_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.addAlarmMenuItem:
                selectAlarmTypeToAdd();
                return true;
                
            case R.id.viewAlarmsMenuItem:
                viewAlarms();
                return true;
                
            case R.id.clockSettingsMenuItem:
                editClockSettings();
                return true;
                
            default:
                break;
        }
        
        return super.onOptionsItemSelected(item);
    }
    
    private void selectAlarmTypeToAdd() {
        AddAlarmTypeSelector selector = new AddAlarmTypeSelector(this);
        selector.promptForAddAlarmType();
    }
    
    private void addAlarm() {
        Intent intent = new Intent(this, AddAlarmActivity.class);
        startActivity(intent);
    }
    
    private void addCountdownTimer() {
        Intent intent = new Intent(this, AddCountdownTimerActivity.class);
        startActivity(intent);
    }
    
    private void viewAlarms() {
        ViewMyAlarmsAction.launchMyAlarmsActivity(this);
    }
    
    private void editClockSettings() {
        Intent intent = new Intent(this, EditClockSettingsActivity.class);
        startActivity(intent);
    }
    
    public static final Context getMainApplicationContext() {
        return mainApplicationContext;
    }
    
    private class AddAlarmTypeSelector implements DialogInterface.OnClickListener {
        public final static int ALARM_INDEX = 0;
        public final static int COUNTDOWN_TIMER_INDEX = 1;
        
        private Context context;
        private String[] alarmTypeLabels;
        private String dialogTitle;
        
        public AddAlarmTypeSelector(Context context) {
            this.context = context;
            initAlarmTypeLabels();
            initDialogTitle();
        }
        
        private void initAlarmTypeLabels() {
            alarmTypeLabels = new String[2];
            alarmTypeLabels[ALARM_INDEX] = getString(R.string.alarm);
            alarmTypeLabels[COUNTDOWN_TIMER_INDEX] = getString(R.string.countdownTimer);
        }
        
        private void initDialogTitle() {
            dialogTitle = getString(R.string.addAlarmMenuTitle);
        }
        
        public void promptForAddAlarmType() {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle(dialogTitle);
            builder.setItems(alarmTypeLabels, this);
            
            AlertDialog dialog = builder.create();
            dialog.show();
        }
        
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case ALARM_INDEX:
                    addAlarm();
                    break;
                    
                case COUNTDOWN_TIMER_INDEX:
                    addCountdownTimer();
                    break;
            }
        }
    }
    
    
}